package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.SaleDAO;
import cn.edu.scau.cmi.hombio.dao.SalePigDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Sale;
import cn.edu.scau.cmi.hombio.domain.SalePig;
import cn.edu.scau.cmi.hombio.domain.Task;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Sale entities
 * 
 */

@Service("SaleService")

@Transactional
public class SaleServiceImpl implements SaleService {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Sale entities
	 * 
	 */
	@Autowired
	private SaleDAO saleDAO;

	/**
	 * DAO injected by Spring that manages SalePig entities
	 * 
	 */
	@Autowired
	private SalePigDAO salePigDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * Instantiates a new SaleServiceImpl.
	 *
	 */
	public SaleServiceImpl() {
	}

	/**
	 * Load an existing Sale entity
	 * 
	 */
	@Transactional
	public Set<Sale> loadSales() {
		return saleDAO.findAllSales();
	}

	/**
	 * Delete an existing Sale entity
	 * 
	 */
	@Transactional
	public void deleteSale(Sale sale) {
		saleDAO.remove(sale);
		saleDAO.flush();
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public Sale deleteSaleEmployee(Integer sale_id, Integer related_employee_id) {
		Sale sale = saleDAO.findSaleByPrimaryKey(sale_id, -1, -1);
		Employee related_employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		sale.setEmployee(null);
		related_employee.getSales().remove(sale);
		sale = saleDAO.store(sale);
		saleDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		employeeDAO.remove(related_employee);
		employeeDAO.flush();

		return sale;
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public Sale saveSaleEmployee(Integer id, Employee related_employee) {
		Sale sale = saleDAO.findSaleByPrimaryKey(id, -1, -1);
		Employee existingemployee = employeeDAO.findEmployeeByPrimaryKey(related_employee.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployee != null) {
			existingemployee.setId(related_employee.getId());
			existingemployee.setNumber(related_employee.getNumber());
			existingemployee.setName(related_employee.getName());
			existingemployee.setPassword(related_employee.getPassword());
			existingemployee.setLogOn(related_employee.getLogOn());
			existingemployee.setRfid(related_employee.getRfid());
			existingemployee.setSex(related_employee.getSex());
			existingemployee.setBirthday(related_employee.getBirthday());
			existingemployee.setState(related_employee.getState());
			existingemployee.setMemo(related_employee.getMemo());
			related_employee = existingemployee;
		}

		sale.setEmployee(related_employee);
		related_employee.getSales().add(sale);
		sale = saleDAO.store(sale);
		saleDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		return sale;
	}

	/**
	 * Return a count of all Sale entity
	 * 
	 */
	@Transactional
	public Integer countSales() {
		return ((Long) saleDAO.createQuerySingleResult("select count(o) from Sale o").getSingleResult()).intValue();
	}

	/**
	 * Return all Sale entity
	 * 
	 */
	@Transactional
	public List<Sale> findAllSales(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Sale>(saleDAO.findAllSales(startResult, maxRows));
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public Sale saveSaleTask(Integer id, Task related_task) {
		Sale sale = saleDAO.findSaleByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		}

		sale.setTask(related_task);
		related_task.getSales().add(sale);
		sale = saleDAO.store(sale);
		saleDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return sale;
	}

	/**
	 */
	@Transactional
	public Sale findSaleByPrimaryKey(Integer id) {
		return saleDAO.findSaleByPrimaryKey(id);
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public Sale deleteSaleTask(Integer sale_id, Integer related_task_id) {
		Sale sale = saleDAO.findSaleByPrimaryKey(sale_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		sale.setTask(null);
		related_task.getSales().remove(sale);
		sale = saleDAO.store(sale);
		saleDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return sale;
	}

	/**
	 * Delete an existing SalePig entity
	 * 
	 */
	@Transactional
	public Sale deleteSaleSalePigs(Integer sale_id, Integer related_salepigs_id) {
		SalePig related_salepigs = salePigDAO.findSalePigByPrimaryKey(related_salepigs_id, -1, -1);

		Sale sale = saleDAO.findSaleByPrimaryKey(sale_id, -1, -1);

		related_salepigs.setSale(null);
		sale.getSalePigs().remove(related_salepigs);

		salePigDAO.remove(related_salepigs);
		salePigDAO.flush();

		return sale;
	}

	/**
	 * Save an existing Sale entity
	 * 
	 */
	@Transactional
	public void saveSale(Sale sale) {
		Sale existingSale = saleDAO.findSaleByPrimaryKey(sale.getId());

		if (existingSale != null) {
			if (existingSale != sale) {
				existingSale.setId(sale.getId());
				existingSale.setSaleType(sale.getSaleType());
				existingSale.setMemo(sale.getMemo());
			}
			sale = saleDAO.store(existingSale);
		} else {
			sale = saleDAO.store(sale);
		}
		saleDAO.flush();
	}

	/**
	 * Save an existing SalePig entity
	 * 
	 */
	@Transactional
	public Sale saveSaleSalePigs(Integer id, SalePig related_salepigs) {
		Sale sale = saleDAO.findSaleByPrimaryKey(id, -1, -1);
		SalePig existingsalePigs = salePigDAO.findSalePigByPrimaryKey(related_salepigs.getId());

		// copy into the existing record to preserve existing relationships
		if (existingsalePigs != null) {
			existingsalePigs.setId(related_salepigs.getId());
			existingsalePigs.setTime(related_salepigs.getTime());
			existingsalePigs.setMemo(related_salepigs.getMemo());
			related_salepigs = existingsalePigs;
		}

		related_salepigs.setSale(sale);
		sale.getSalePigs().add(related_salepigs);
		related_salepigs = salePigDAO.store(related_salepigs);
		salePigDAO.flush();

		sale = saleDAO.store(sale);
		saleDAO.flush();

		return sale;
	}
}
